package com.aiju.cash.core.api;

import com.aiju.app.DataProviderManager;
import com.aiju.app.config.ConfigManager;
import com.aiju.utils.L;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import static com.zhy.http.okhttp.OkHttpUtils.post;

/**
 * @author: 佐为
 * @date: 2016-11-23 15:25
 * @version: V1.0
 * @Description: 登录
 */
public class LoginApi {

    private String register = ConfigManager.INIT_SERVER_HOST + "/sso/register";
    private String getAuthCode = ConfigManager.INIT_SERVER_HOST + "/note/get";
    private String checkAuthCode = ConfigManager.INIT_SERVER_HOST + "/note/check";
    private String login = ConfigManager.INIT_SERVER_HOST + "/sso/login";
    private String newpassword = ConfigManager.INIT_SERVER_HOST + "/sso/newPassword";
    private String logout = ConfigManager.INIT_SERVER_HOST + "/sso/logout";
    private String updatePassword = ConfigManager.INIT_SERVER_HOST + "/sso/updatePassword";

    private String update = ConfigManager.INIT_SERVER_HOST + "/user/update";
    private String list = ConfigManager.INIT_SERVER_HOST + "/user/list";
    private String delete = ConfigManager.INIT_SERVER_HOST + "/user/delete";
    private String invite = ConfigManager.INIT_SERVER_HOST + "/user/inviteByNote";

    private String jpush = ConfigManager.INIT_SERVER_HOST + "/jpush/bind";

    private String updateUserOnlineStatus = ConfigManager.INIT_SERVER_HOST + "/sso/updateUserOnlineStatus";


    /**
     * 用户注册
     */
    public void register(String userName, String nickName, String password, StringCallback callback) {
        L.d("mima:" + password);
        post()
                .url(register)
                .addParams("userName", userName)
                .addParams("nickName", nickName)
                .addParams("password", password)
                .build()
                .execute(callback);
    }

    /**
     * 获取验证码
     *
     * @param type 类型 1:注册 2:修改密码
     */
    public void getAuthCode(String phone, String type, StringCallback callback) {
        post()
                .url(getAuthCode)
                .addParams("phone", phone)
                .addParams("type", type)
                .build()
                .execute(callback);
    }

    /**
     * 检查验证码
     */
    public void checkAuthCode(String phone, String code, StringCallback callback) {
        post()
                .url(checkAuthCode)
                .addParams("phone", phone)
                .addParams("code", code)
                .build()
                .execute(callback);
    }

    /**
     * 设置密码
     */
    public void setNewPassword(String userName, String newPassword, String code, StringCallback callback) {
        post()
                .url(newpassword)
                .addParams("userName", userName)
                .addParams("newPassword", newPassword)
                .addParams("code", code)
                .build()
                .execute(callback);
    }

    /**
     * 登录
     *
     * @param userName
     * @param password
     * @param callback
     */
    public void login(String userName, String password, String equipmentCode, StringCallback callback) {
        post()
                .url(login)
                .addParams("userName", userName)
                .addParams("password", password)
                .addParams("equipmentCode", equipmentCode)
                .addParams("phoneType", DataProviderManager.getDataProvider().getModel())
                .addParams("phoneId", DataProviderManager.getDataProvider().getPhoneId())
                .addParams("OS", "android")
                .build()
                .execute(callback);
    }


    /**
     * 注销
     *
     * @param token
     * @param callback
     */
    public void logout(String token, String equipmentCode, StringCallback callback) {
        post()
                .url(logout)
                .addParams("token", token)
                .addParams("equipmentCode", equipmentCode)
                .addParams("phone", ConfigManager.getInstance().getUser().getPhone())
                .addParams("id", ConfigManager.getInstance().getUser().getId())
                .addParams("storeId", ConfigManager.getInstance().getUser().getStoreId())
                .build()
                .execute(callback);
    }

    /**
     * 修改密码
     *
     * @param userName
     * @param oldPassword
     * @param newPassword
     * @param callback
     */
    public void updatePassword(String userName, String oldPassword, String newPassword, StringCallback callback) {
        post()
                .url(updatePassword)
                .addParams("userName", userName)
                .addParams("oldPassword", oldPassword)
                .addParams("newPassword", newPassword)
                .build()
                .execute(callback);
    }

    /**
     * 用户修改
     *
     * @param id
     * @param token
     * @param userName
     * @param callback
     */
    public void userUpdate(String id, String token, String userName, String storeId, StringCallback callback) {
        post()
                .url(update)
                .addParams("id", id)
                .addParams("token", token)
                .addParams("userName", userName)
                .addParams("storeId", storeId)
                .build()
                .execute(callback);
    }

    /**
     * 用户列表
     *
     * @param storeId
     * @param token
     * @param callback
     */
    public void userlist(String storeId, String token, StringCallback callback) {
        post()
                .url(list)
                .addParams("storeId", storeId)
                .addParams("token", token)
                .build()
                .execute(callback);
    }

    /**
     * 删除用户
     *
     * @param operatorId 用户id
     * @param token
     * @param callback
     */
    public void userDelete(String operatorId, String token, StringCallback callback) {
        post()
                .url(delete)
                .addParams("operatorId", operatorId)
                .addParams("token", token)
                .build()
                .execute(callback);
    }

    /**
     * 邀请用户
     *
     * @param phone
     * @param token
     * @param callback
     */
    public void inviteUser(String phone, String storeId, String token, StringCallback callback) {
        post()
                .url(invite)
                .addParams("phone", phone)
                .addParams("storeId", storeId)
                .addParams("token", token)
                .build()
                .execute(callback);
    }

    /**
     * 用户列表
     *
     * @param storeId
     * @param operatorId
     * @param equipmentCode
     * @param token
     * @param callback
     */
    public void jpushBind(String storeId, String operatorId, String equipmentCode, String phone, String token, StringCallback callback) {
        L.d("极光id:" + equipmentCode);
        OkHttpUtils
                .post()
                .url(jpush)
                .addParams("storeId", storeId)
                .addParams("operatorId", operatorId)
                .addParams("equipmentCode", equipmentCode)
                .addParams("phoneType", DataProviderManager.getDataProvider().getModel())
                .addParams("phone", phone)
                .addParams("token", token)
                .build()
                .execute(callback);
    }


    public void updateUserOnlineStatus(StringCallback callback) {
        post()
                .url(updateUserOnlineStatus)
                .addParams("storeId", ConfigManager.getInstance().getUser().getStoreId())
                .addParams("phone", ConfigManager.getInstance().getUser().getPhone())
                .addParams("currentStatus", "0")
                .addParams("token", ConfigManager.getInstance().getUser().getToken())
                .build()
                .execute(callback);
    }
}
